<?php
/**
 * Created by PhpStorm.
 * Script Name: Setting.php
 * Create: 2022/12/22 14:31
 * Description:
 * Author: fudaoji<fdj@kuryun.cn>
 */

namespace plugin\wechat\app\service;
use plugin\wechat\app\model\Setting as SettingM;

class Setting
{
    static $model = null;

    static function model(){
        if(is_null(self::$model)){
            self::$model = new SettingM();
        }
        return self::$model;
    }

    /**
     * 获取配置
     * @param null $name
     * @param bool $refresh
     * @return array|mixed
     * @throws \Psr\SimpleCache\InvalidArgumentException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * Author: fudaoji<fdj@kuryun.cn>
     */
    public static function getSettings($name = null, $refresh = false){
        $cache_key = wechat_cache_key((is_null($name) ? 'all' : $name));

        $list = cache($cache_key);
        if(empty($list) || $refresh){
            $list = self::model()->column('value', 'name');
            foreach ($list as $k => &$v){
                $v = json_decode($v, true);
            }
        }
        cache($cache_key, $list);
        return isset($list[$name]) ? $list[$name] : $list;
    }
}